<?php

App::uses('AppController', 'Controller');
App::uses('Category', 'Model');

class KardexController extends AppController {

    public function index ($nomina = null) {
        if(!$nomina) {
            $info = $this->getInfoForKardex($this->Auth->user());
            $title = __('Mi Kardex');
        } else {
            if ($this->Auth->user('category_id') == Category::ADMIN || $this->Auth->user('category_id') == Category::KAOISAMA) {
                $this->loadModel('User');
                $user = $this->User->findByNomina($nomina);
                $info = $this->getInfoForKardex($user['User']);
                $title = __('Kardex');
            }
        }
        $this->set('info', $info);
        $this->set('title', $title);
        $this->set('description', 'Historial de cursos y certificaciones');
    }

    public function admin() {
        $appBosch = new stdClass();
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Ver Kardex'));
        $this->set('description', 'Historial de cursos y certificaciones');
    }

    public function kardex($nomina) {
        $this->request->onlyAllow('get');
        $this->loadModel('User');
        $user = $this->User->findByNomina($nomina);
        if(isset($user['User'])) {
            if($user['User']['status'] == User::STATUS_ENABLED) {
                if($user['User']['category_id'] == Category::ASOCIADO) {
                    $info = $this->getInfoForKardex($user['User']);
                } else {
                    $info = __('Error:El numero de nomina no es de un asociado');
                }
            } else {
                $info = __('Error:Este asociado no esta activo');
            }
        } else {
            $info = __('Error:El numero de nomina proporcionado no existe o es inválido');
        }
        $this->set('info', $info);
        $this->layout = 'ajax';
    }

    private function getInfoForKardex($user) {
        $this->loadModel('Certification');
        $this->loadModel('Attempt');
        $this->loadModel('UserTest');
        $this->loadModel('Theme');
        $this->loadModel('UserLine');
        $info = array();
        $info['user'] = $user;
        $info['certifications'] = $this->Certification->getCertificationsForKardex($user['id']);
        $info['trainings'] = array();
        $info['trainings']['calificated'] = $this->Attempt->getAtteptsForKardex($user['id']);
        $info['trainings']['training'] = $this->UserTest->getTestsForKardex($user['id']);
        $tam = count($info['trainings']['calificated']);
        for ($i=0; $i < $tam; $i++) {
            $info['trainings']['calificated'][$i]['themes'] = $this->Theme->getThemesByTestId($info['trainings']['calificated'][$i]['test']);
        }
        $tam = count($info['trainings']['training']);
        for ($i=0; $i < $tam; $i++) {
            $info['trainings']['training'][$i]['themes'] = $this->Theme->getThemesByTestId($info['trainings']['training'][$i]['test']);
        }
        $info['summary'] = $this->UserLine->getSummaryForKardex($user['id']);

        return $info;
    }
}